package com.freejy.ecommunity.service;

import com.freejy.ecommunity.common.Constants;
import com.freejy.ecommunity.mapper.AccountMapper;
import com.freejy.ecommunity.model.Account;
import com.freejy.ecommunity.utils.MD5Builder;
import com.freejy.ecommunity.utils.StrUtil;
import com.freejy.ecommunity.web.dto.request.ModifyUserReqDto;
import com.freejy.ecommunity.web.dto.request.RegisterReqDto;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class AccountService {

    private final Logger log = LoggerFactory.getLogger(AccountService.class);
    

    @Autowired
    private AccountMapper accountMapper;
    

    /**
     * 注册用户.
     * @param udt
     * @return
     * @throws Exception 
     */
    public boolean registerUser(RegisterReqDto udt) throws Exception
    {
        boolean res = false;
        if (udt != null)
        {
            String uuid = StrUtil.getUUID();
            Account user = new Account();
            user.setNickname(udt.getAccount());
            user.setUuid(uuid);
            String encryptedPassword = MD5Builder.getMD5(Constants.SI_SAL + udt.getPassword());
            user.setPassword(encryptedPassword);
            user.setLevel(Constants.DEFAULT_LEVEL.toString());
            int rct = accountMapper.insertSelective(user);
            res = rct > 0 ? true : false;
        }
        return res;
    }

    /**
     * 更新用户.
     */
    public boolean update(Long accountId, ModifyUserReqDto dto) throws Exception
    {
        Account model = new Account();
        if (StringUtils.isNotBlank(dto.getNickname()))
        {
        	model.setNickname(dto.getNickname());
        }
        if (StringUtils.isNotBlank(dto.getAvatar()))
        {
        	model.setAvatar(dto.getAvatar());
        }
        if (dto.getGender() != null)
        {
        	model.setGender(dto.getGender());
        }
        if (dto.getBirthday() != null)
        {
            model.setBirthday(dto.getBirthdayDate());
        }
        if (StringUtils.isNotBlank(dto.getAddress()))
        {
            model.setAddress(dto.getAddress());
        }
        if (StringUtils.isNotBlank(dto.getInstr()))
        {
            model.setInstr(dto.getInstr());
        }
        model.setId(accountId);
        return update(model);
    }

    /**
     * 根据登陆名查询
     * @param account
     * @return
     * @throws Exception
     */
	public Account findByAccount(String account) throws Exception
    {
        Account model = null;
        if (StringUtils.isNotBlank(account))
        {
            model = accountMapper.findByAccount(account);
        }
		return model;
	}

    public List<Account> findAll(Long uid) throws Exception
    {
        List<Account> list = accountMapper.findAll();
        if (uid != null && uid.longValue() > 0)
        {
            list.removeIf(obj -> obj.getId().longValue() == uid.longValue());
        }
        return list;
    }


    /**
     * 根据UUID查询用户信息
     * @param uuid
     * @return
     * @throws Exception
     */
	public Account findByUuid(String uuid) throws Exception
    {
        Account model = null;
        if (StringUtils.isNotBlank(uuid))
        {
            model = accountMapper.findByUuid(uuid);
        }
		return model;
	}

    /**
     * 根据ID查询用户信息
     * @param id
     * @return
     */
	public Account findById(Long id) throws Exception
    {
        Account user = null;
        if (id != null)
        {
            user = accountMapper.findById(id);
        }
		return user;
	}

    /**
     * 根据手机号查询用户信息
     * @param phone
     * @return
     * @throws Exception
     */
	public Account findByPhone(String phone, Long uid) throws Exception
    {
        Account model = null;
        if (StringUtils.isNotBlank(phone))
        {
            model = accountMapper.findByPhone(phone, uid);
        }
		return model;
	}

    /**
     * 根据Email查询
     * @param email
     * @return
     * @throws Exception
     */
	public Account findByEmail(String email, Long uid) throws Exception
    {
		Account model = null;
		if (StringUtils.isNotBlank(email))
        {
            model = accountMapper.findByEmail(email, uid);
		}
		return model;
	}

    /**
     * 根据用户昵称查询用户信息
     * @param nickname
     * @param uid
     * @return
     * @throws Exception
     */
	public Account findByNickname(String nickname, Long uid) throws Exception
    {
		Account model = null;
		if (StringUtils.isNotBlank(nickname))
        {
            model = accountMapper.findByNickname(nickname, uid);
		}
		return model;
	}

    /**
     * 更新用户信息
     * @param user
     * @return
     */
	public boolean update(Account user)
    {
        boolean res = false;
        if (user != null)
        {
            int ct = accountMapper.updateByPrimaryKeySelective(user);
            res = ct>0?true:false;
        }
        return res;
	}

    /**
     * 保存用户信息
     * @param user
     * @return
     */
	public boolean saveUser(Account user) throws Exception
    {
        boolean res = false;
        if (user != null)
        {
            int ct = accountMapper.insertSelective(user);
            res = ct>0?true:false;
        }
		return res;
	}

}
